<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改费用核销')" />
    <th:block th:include="include :: datetimepicker-css" />
    <style type="text/css">
        table label.error{position: inherit;}select + label.error{z-index:1;right:40px;}
    </style>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-fyhx-edit" th:object="${gcFyhx}">
        	<h4 class="form-header h4">基本信息</h4>
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-1 control-label is-required">类型：</label>
                <div class="col-sm-2">
                	<select name="type" class="form-control" th:field="*{type}" th:with="type=${@dict.getType('fyhx_type')}" required>
					  <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictLabel}">
					  </option>
					</select>
                </div>
            	<label class="col-sm-1 control-label is-required">报销人：</label>
                <div class="col-sm-2">
                    <input name="bxr" th:field="*{bxr}" class="form-control" type="text" autoComplete="off" required>
                </div> 
                <label class="col-sm-1 control-label is-required">报销时间：</label>
                <div class="col-sm-2">
                    <div class="input-group date">
                        <input name="bxrAt" th:value="${#dates.format(gcFyhx.bxrAt, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" autoComplete="off" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
<!--                 <label class="col-sm-1 control-label is-required">发票数量：</label> -->
<!--                 <div class="col-sm-2"> -->
<!--                     <input name="djsl" th:field="*{djsl}" class="form-control" type="text" autoComplete="off" required> -->
<!--                 </div> -->
            </div>
            <h4 class="form-header h4">表单项</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button th:if="${gcFyhx.status} eq 0" type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>
                    <button th:if="${gcFyhx.status} eq 0" type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
					    <table id="bootstrap-table"></table>
					</div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button th:if="${gcFyhx.status} eq 0" type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "gcgl/fyhx";
        $("#form-fyhx-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
            	var data = $("#form-fyhx-add").serializeArray();
                $.operate.saveTab(prefix + "/edit", $('#form-fyhx-edit').serialize());
            }
        }

        $("input[name='bxrAt']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function() {
        	// 初始化数据, 可以由后台传过来
	    	var data = [[${gcFyhx.item}]];
        	
	    	// 初始化数据, 可以由后台传过来
		    var options = {
		    	data: data,
                pagination: false,
		        showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
		        columns: [
		        {
		        	field: 'index',
		        	align: 'center',
		        	title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	var columnId = $.common.sprintf("<input type='hidden' name='item[%s].xuh' value='%s'>", index, row.id);
                    	return columnIndex + $.table.serialNumber(index) + columnId;
                    }
                },
		        {
		            field: 'descs',
		            align: 'center',
		            title: '费用类型',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control itemdescs' type='text' autoComplete='off' name='item[%s].descs' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'type',
		            align: 'center',
		            title: '发票类型',
		            width:180,
		            formatter: function(value, row, index) {
		        		var data = [{ index: index, type: value }];
		                return $("#itemTypeTpl").tmpl(data).html();
                    }
		        },
		        {
		            field: 'jine',
		            align: 'center',
		            title: '金额',
		            width:180,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control itemjine' type='text' autoComplete='off' name='item[%s].jine' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'memo',
		            align: 'center',
		            title: '备注',
		            width:280,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control' type='text' autoComplete='off' name='item[%s].memo' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
                    title: '操作',
                    align: 'center',
                    width:80,
                    formatter: function(value, row, index) {
                    	var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
		    };
		    $.table.init(options);
		});
        
        $.validator.addMethod("itemtype", function(value, element) {
            return !this.optional(element);
        }, "类型必填。");
        $.validator.addMethod("itemdescs", function(value, element) {
            return !this.optional(element);
        }, "事项必填。");
        $.validator.addMethod("itemjine", function(value, element) {
            return !this.optional(element) && (value <= 10000000 && value >= 1);
        }, "金额长度区间1-10000000。");
        
        function addRow() {
        	var count = $("#" + table.options.id).bootstrapTable('getData').length;
        	var row = {
                index: $.table.serialNumber(count),
                descs: "",
                type: "",
                jine: "",
                memo: ""
            }
        	sub.addRow(row);
        }
    </script>
</body>
</html>

<!-- 成本类型 -->
<script id="itemTypeTpl" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='item[${index}].type'>
    <option value="普通发票" {{if type==="普通发票"}}selected{{/if}}>普通发票</option>
	<option value="专用发票" {{if type==="专用发票"}}selected{{/if}}>专用发票</option>
<option value="其他选项" {{if type==="其他选项"}}selected{{/if}}>其他选项</option>
</select>
</div>
</script>